"use strict"
//日常数据分布
let mvc = require("cosjs.mvc");

exports.page = function () {
    let page = this.get('page', 'int') || 1;
    let size = this.get('size', 'int') || 20;

    let skip = (page - 1) * size, limit = size;
    let data = { "page": page, "size": size, "total": 0, "records": 0, "rows": [] };
    let format = mvc.format("daily");
    let jqgrid = this.library.call(this, 'jqgrid/query', this.req.body, format, 1);
    let query = { 'time': jqgrid['time'] || this.library("time/sign") };

    let key = ['$', jqgrid['key'] || 'login'].join('');


    let group = { "_id": key, "val": { "$sum": 1 } }, options = { "dataType": "cursor" };
    let pipeline = [{ $match: query }, { $group: group }];
    pipeline.push({ "$sort": { "_id": 1 } });

    let daily = this.model.mongo('daily', this.sid);

    return daily.aggregate(pipeline, options).then(cursor => {
        return cursor.skip(skip).limit(limit).toArray();
    }).then(rows => {
        data['rows'] = rows || [];
        data['records'] = data['rows'].length;
        data['total'] = Math.ceil(data['records'] / data['size']);
        for (let v of data['rows']) {
            v['_id'] = v['_id'] || 0;
            v['key'] = key.substr(1);
            v['time'] = query['time'];
        }
        return data;
    })

}


exports.save = function () {
    return this.error("count.battle save not exist ");
}


